import 'package:flutter/material.dart';
import 'package:mock_douban/model/home/home_requests_model.dart';
import 'package:mock_douban/pages/home/home_bilibili_hotsearch_detail.dart';
import 'package:mock_douban/service/home/home_requests.dart';

import '../../widgets/bilibili_hotsearch_item.dart';


class DBHomeContents extends StatefulWidget {
  const DBHomeContents({super.key});



  @override
  State<DBHomeContents> createState() => _DBHomeContentsState();
}

class _DBHomeContentsState extends State<DBHomeContents> {

   List<HomeBilibiliHotSearchModel> hotSearch=[];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    // 请求初始化数据
    fetchData();
  }

  Future<void> fetchData() async {
    try {
      final models = await HomeBilibiliHotSearch.requestBilibiliHotSearchList();
      setState(() {
        hotSearch = models;
      });
    } catch (e) {
      print('Error fetching data: $e');
    }
  }


   void _navigateToDetail(HomeBilibiliHotSearchModel model) {
    print("model:--${model.title}");
     Navigator.push(
       context,
       MaterialPageRoute(
         builder: (context) => HotSearchDetailPage(model: model),
       ),
     );
   }
  
  @override
  Widget build(BuildContext context) {
    if (hotSearch.isEmpty) {
      return const Center(child: CircularProgressIndicator());
    } else {
      return ListView.builder(
        itemCount: hotSearch.length,
        itemBuilder: (context, index) {
          return BilibiliHotSearchItem(
            model: hotSearch[index],
            onTap: () => _navigateToDetail(hotSearch[index]),
          );
        },
      );
    }
  }
}
